﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.GoodsModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmSaleDataQuery : Form
    {
        public FrmSaleDataQuery()
        {
            InitializeComponent();
        }

        string selectCondition = "SaleID";

        void TxtClear()
        {
            txtSaleID.Text = "";
            txtGoodsID.Text = "";
            txtName.Text = "";
            txtInPrice.Text = "";
            txtOutPrice.Text = "";
            txtQuantity.Text = "";
            txtTotalPrice.Text = "";
            txtEarnings.Text = "";
            txtDate.Text = "";
            txtSeller.Text = "";
        }

        void SetHeaderText()
        {
            dgrdvSale.Columns[0].HeaderText = "单号";
            dgrdvSale.Columns[0].Width = 56;
            dgrdvSale.Columns[1].HeaderText = "商品号";
            dgrdvSale.Columns[1].Width = 94;
            dgrdvSale.Columns[2].HeaderText = "商品名";
            dgrdvSale.Columns[2].Width = 120;
            dgrdvSale.Columns[3].HeaderText = "进价";
            dgrdvSale.Columns[3].Width = 60;
            dgrdvSale.Columns[4].HeaderText = "售价";
            dgrdvSale.Columns[4].Width = 60;
            dgrdvSale.Columns[5].HeaderText = "数量";
            dgrdvSale.Columns[5].Width = 60;
            dgrdvSale.Columns[6].HeaderText = "总价";
            dgrdvSale.Columns[6].Width = 60;
            dgrdvSale.Columns[7].HeaderText = "利润";
            dgrdvSale.Columns[7].Width = 60;
            dgrdvSale.Columns[8].HeaderText = "销售日期";
            dgrdvSale.Columns[8].Width = 100;
            dgrdvSale.Columns[9].HeaderText = "销售员";
            dgrdvSale.Columns[9].Width = 80;
        }

        void RefreshData()
        {
            SaleManager saleManager = new SaleManager();
            List<Sale> list = saleManager.GetSales();
            dgrdvSale.DataSource = list;
            if (list.Count == 0)
            {
                TxtClear();
            }
            SetHeaderText();
        }

        private void FrmSaleDataQuery_Load(object sender, EventArgs e)
        {
            try
            {
                RefreshData();
                cmbSelect.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dgrdvSale_CurrentCellChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgrdvSale.CurrentCell == null)
                {
                    return;
                }
                int n = dgrdvSale.CurrentCell.RowIndex;
                dgrdvSale.Rows[n].Selected = true;
                txtSaleID.Text = dgrdvSale.Rows[n].Cells[0].Value.ToString();
                txtGoodsID.Text = dgrdvSale.Rows[n].Cells[1].Value.ToString();
                txtName.Text = dgrdvSale.Rows[n].Cells[2].Value.ToString();
                txtInPrice.Text = dgrdvSale.Rows[n].Cells[3].Value.ToString();
                txtOutPrice.Text = dgrdvSale.Rows[n].Cells[4].Value.ToString();
                txtQuantity.Text = dgrdvSale.Rows[n].Cells[5].Value.ToString();
                txtTotalPrice.Text = dgrdvSale.Rows[n].Cells[6].Value.ToString();
                txtEarnings.Text = dgrdvSale.Rows[n].Cells[7].Value.ToString();
                txtDate.Text = dgrdvSale.Rows[n].Cells[8].Value.ToString();
                txtSeller.Text = dgrdvSale.Rows[n].Cells[9].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmbSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSelect.SelectedIndex == 0)
            {
                selectCondition = "SaleID";
            }
            else
            {
                selectCondition = "GoodsID";
            }
            txtSelect.Text = "";
            txtSelect.Focus();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSelect.Text.Trim() == "")
                {
                    MessageBox.Show("请输入查询值", "提示");
                    txtSelect.Focus();
                    return;
                }
                string selectValue = txtSelect.Text.Trim();
                if (cmbSelect.SelectedIndex == 0 && int.Parse(selectValue) > 2147483647)
                {
                    txtSelect.Text = "";
                    txtSelect.Focus();
                    return;
                }
                SaleManager saleManager = new SaleManager();
                List<Sale> list = saleManager.GetSales(selectCondition, selectValue);
                dgrdvSale.DataSource = list;
                if (list.Count == 0)
                {
                    TxtClear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtSelect_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSelect_Click(sender, e);
            }
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            try
            {
                RefreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
